package cn.com.ghy.android.bean;

import java.io.InputStream;
import java.io.Writer;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;
import cn.com.ghy.android.util.XMLStatic;

public class OrderSellReportBeanParser {
	/**
	 * 创建查询月销售报表XML条件
	 * @param monthid 月份ID
	 * @param writer
	 * @return
	 * @throws Exception
	 */
	public static String createXMLOrderShellReportQuery(String monthid, Writer writer)
			throws Exception {
		XmlSerializer xmlSerializer = Xml.newSerializer();

		xmlSerializer.setOutput(writer);
		xmlSerializer.startDocument("UTF-8", true);
		xmlSerializer.startTag("", XMLStatic.CONTENT);
		xmlSerializer.attribute("", XMLStatic.TOTAL_RECORD, "1");
		xmlSerializer.startTag("", XMLStatic.RECORDS);

		xmlSerializer.startTag("", XMLStatic.MONTHID);
		xmlSerializer.text(monthid);
		xmlSerializer.endTag("", XMLStatic.MONTHID);
		xmlSerializer.startTag("", XMLStatic.ISALL);
		xmlSerializer.text("1");
		xmlSerializer.endTag("", XMLStatic.ISALL);

		xmlSerializer.endTag("", XMLStatic.RECORDS);
		xmlSerializer.endTag("", XMLStatic.CONTENT);
		xmlSerializer.endDocument();
		writer.flush();
		writer.close();
		return writer.toString().replace(
				"<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>", "");
	}

	/**
	 * 销售月报表解析类
	 * @param in
	 * @return
	 * @throws Exception
	 */
	public static OrderSellReportBean parseXML(InputStream in) throws Exception {
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(in, "UTF-8");
		int type = parser.getEventType();

		OrderSellReportBean bean = null;
		LoginInfoBean loginInfo = null;

		while (type != XmlPullParser.END_DOCUMENT) {
			switch (type) {
				case XmlPullParser.START_DOCUMENT :
					break;

				case XmlPullParser.START_TAG :
					if ("Table".equals(parser.getName())) {
						bean = new OrderSellReportBean();
						loginInfo = new LoginInfoBean();
					} else if (null != bean && null != loginInfo) {
						if ("PERNR".equals(parser.getName())) {
							loginInfo.setPernr(parser.nextText());
						} else if ("PERNRNAME".equals(parser.getName())) {
							loginInfo.setDspname(parser.nextText());
						} else if ("SENDORDER".equals(parser.getName())) {
							bean.setSendorder(parser.nextText());
						} else if ("PARTNERNUM".equals(parser.getName())) {
							bean.setPartnernum(parser.nextText());
						} else if ("SENDNUM".equals(parser.getName())) {
							bean.setSendnum(parser.nextText());
						} else if ("ORDERNUM".equals(parser.getName())) {
							bean.setOrdernum(parser.nextText());
						} else if ("TOTALNUM".equals(parser.getName())) {
							bean.setTotalnum(parser.nextText());
						} else if ("TOTALMONEY".equals(parser.getName())) {
							bean.setTotalmoney(parser.nextText());
						} else if ("SENDMONEY".equals(parser.getName())) {
							bean.setSendmoney(parser.nextText());
						} else if ("TOTALFPMONEY".equals(parser.getName())) {
							bean.setTotalfpmoney(parser.nextText());
						} else if ("SENDFPMONEY".equals(parser.getName())) {
							bean.setSendfpmoney(parser.nextText());
						} else if ("PERORDERNRT".equals(parser.getName())) {
							bean.setPerordernrt(parser.nextText());
						} else if ("PERSENDNRT".equals(parser.getName())) {
							bean.setPersendnrt(parser.nextText());
						} else if ("SALETYPE".equals(parser.getName())) {
							bean.setSaletype(parser.nextText());
						} else if ("SALETYPENAME".equals(parser.getName())) {
							bean.setSaletypename(parser.nextText());
						} else if ("SALEMONEY".equals(parser.getName())) {
							bean.setSalemoney(parser.nextText());
						} else if ("SALEFPMONEY".equals(parser.getName())) {
							bean.setSalefpmoney(parser.nextText());
						} else if ("PERNUM".equals(parser.getName())) {
							bean.setPernum(parser.nextText());
						} else if ("PERMONEY".equals(parser.getName())) {
							bean.setPermoney(parser.nextText());
						} else if ("PERSALE".equals(parser.getName())) {
							bean.setPersale(parser.nextText());
						} else if ("PERFPSALE".equals(parser.getName())) {
							bean.setPerfpsale(parser.nextText());
						}
					}
					break;

				case XmlPullParser.END_TAG :
					if ("Table".equals(parser.getName())) {
						if (null != bean && null != loginInfo) {
							bean.setLoginInfoBean(loginInfo);
						}
					}
					break;
			}
			type = parser.next();
		}
		in.close();
		return bean;
	}
}
